/**
 * \file addr_utils.c
 * \brief Outils pour les adresses
 * \author Pierre Neumann / Julien Meutelet
 * \version 0.1
 * \date 20 decembre 2013
 *
 * Permet de transformer un nom d'hote en adresse IP
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>

#include "addr_utils.h"

/*
int is_ipv4_or6(char *addr){
	struct addrinfo hint, *info = 0;
	memset(&hint, 0, sizeof(hint));
	hint.ai_family = AF_UNSPEC;
	int ret = getaddrinfo(addr, 0, &hint, &info);
	if(ret) return -1;
	int res = info->ai_family;
	free(info);
	return res+12;
}*/

/**
 * \fn get_first_ip_by_hostname(char* hostname)
 * \brief Renvoie la première adresse ip d'un hostname.
 *
 * \param hostname Nom d'hote du serveur (ex: google.com)
 * \return Adresse IP.
 */
char* get_first_ip_by_hostname(char* hostname){ 
	struct hostent *ho;
	struct in_addr **addr_list;	
	if ((ho=gethostbyname(hostname)) == NULL){
		perror("gethostbyname");
		return NULL;
	}
	addr_list = (struct in_addr **) ho->h_addr_list;
	int i;
	for(i=0;addr_list[i] != NULL;i++){
		return inet_ntoa(*addr_list[i]);
	}
	return NULL;
}
